package com.yiren.manager.ctrl.pension;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrNursingCostHistory;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.pension.YrNursingCostHistoryForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.pension.YrNursingCostHistoryService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("/nursingCostHistory")
@Controller
public class YrNursingCostHistoryController extends GenericController<YrNursingCostHistoryForm, YrNursingCostHistory>{

	@Autowired
	private YrNursingCostHistoryService yrNursingCostHistoryService;
	
	@Autowired
	private YrStaffInfoService yrStaffInfoService;
	
	/**
	 * @param nursingCostId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrNursingCostHistoryForm> find(String nursingCostId) throws Exception{
		HTTPResult<YrNursingCostHistoryForm> result = new HTTPResult<YrNursingCostHistoryForm>();
		if(StringUtil.isEmpty(nursingCostId)){
			throwExp("param.error");
		}
		String sqlWhere = "nursing_cost_id=?1";
		 
		QueryResult<YrNursingCostHistory> qr = yrNursingCostHistoryService.find(YrNursingCostHistory.class, sqlWhere, new Object[]{nursingCostId});
		
		List<YrNursingCostHistoryForm> formList = new ArrayList<YrNursingCostHistoryForm>();
		for(YrNursingCostHistory entity : qr.getResultList()){
			YrNursingCostHistoryForm form = toForm(entity);
			if(null!=entity.getUpdateId()){
				YrStaffInfo staffInfo = yrStaffInfoService.findOne(entity.getUpdateId());
				if(staffInfo!=null){
					form.setUpdateName(staffInfo.getName());
				}
			}
			formList.add(form);
		}	
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrNursingCostHistoryForm toForm(YrNursingCostHistory e) throws Exception {
		YrNursingCostHistoryForm form = ObjectUtils.objectToObject(e, YrNursingCostHistoryForm.class);
		return form;
	}

	@Override
	public YrNursingCostHistory toEntity(YrNursingCostHistoryForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}



}
